﻿using System;
using System.Data;
using System.Data.SqlClient;
using Dtl;
using System.Collections.Generic;
using System.Collections;

namespace Dal
{
    public class RoleGrantDao
    {

        public static int setRoleGrantt(Role role , List<RoleGrant> ListRoleGrant) 
        {            

            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }

            int insert = 0;
            int idRole = 0;
            SqlTransaction tr = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            try
            {
                cmd.Connection.Open();
                cmd.Connection.CreateCommand();
                tr = cmd.Connection.BeginTransaction("Transaction");
                cmd.Transaction = tr;

                cmd.CommandType = System.Data.CommandType.StoredProcedure;




                if (role.typeOperation == true)//CREAR ROLE
                {
                    cmd.CommandText = "dbo.dms_Set_Role";
                    cmd.Parameters.AddWithValue("@cdRole", role.cdRole);
                    cmd.Parameters.AddWithValue("@dsName", role.dsName);
                   
                    cmd.Parameters["@idRole"].Direction = ParameterDirection.Output;
                   
                    insert = cmd.ExecuteNonQuery();
                    idRole = int.Parse(cmd.Parameters["@idRole"].Value.ToString());
                    
                }
                else//ACTUALIZA ROLE
                {
                    cmd.CommandText = "dbo.dms_Set_Role_Change";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@idRole", role.idRole);
                    cmd.Parameters.AddWithValue("@dsName", role.dsName);                 
                    insert = cmd.ExecuteNonQuery();
                 
                    //ELIMINAR PERMISOS  PARA EL PERFIL
                    cmd.CommandText = "dbo.dms_Delete_Grantt";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@idRole", role.idRole);
                    insert = cmd.ExecuteNonQuery();
                    
                }


                if (idRole == 0) 
                {
                    idRole = role.idRole;
                }
                Credentials.writeLogFile("idRole" + idRole);
                Credentials.writeLogFile("ListRoleGrant " + ListRoleGrant.Count); 
                foreach (RoleGrant roleGrant in ListRoleGrant)
                {
                    // CREAR PERMISO
                    cmd.CommandText = "dbo.dms_Set_Grantt";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@idRole", idRole);
                    cmd.Parameters.AddWithValue("@idObjeto", roleGrant.idObject);
                    insert = cmd.ExecuteNonQuery();
                    Credentials.writeLogFile("insert " + insert); 
                }

                tr.Commit();
                cmd.Connection.Close();
                return insert;
            }
            catch (Exception exc)
            {
                tr.Rollback();
                cmd.Connection.Close();
                Credentials.writeLogFile("setRoleGrantt: " + exc.Message);
                return -1;
            }

             
        }

        public static DataSet getObjects()
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Get_Objects";          
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtVisit = new DataTable("Objectsdt");

            try
            {
                cmd.Connection.Open();
                da.Fill(dtVisit);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getObjects: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("Objectsds");
            ds.Tables.Add(dtVisit);

            return ds;
        
        }

        public static DataSet getObjectsByRole(string cdRole)
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Get_ObjectsByRole";
            cmd.Parameters.AddWithValue("@cdRole", cdRole);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtVisit = new DataTable("ObjectsByRoledt");

            try
            {
                cmd.Connection.Open();
                da.Fill(dtVisit);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getObjectsByRole: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("ObjectsByRoleds");
            ds.Tables.Add(dtVisit);

            return ds;

        }
    }

}
